package com.pucong.gitee.database.backup.database.util;

import com.pucong.gitee.database.backup.common.entity.BackupDatabase;
import com.pucong.gitee.database.backup.common.entity.DatabaseTable;
import com.pucong.gitee.database.backup.common.selfenum.DatabaseDriverClassEnum;
import com.pucong.gitee.database.backup.configuration.exception.BusinessException;
import com.pucong.gitee.database.backup.database.mapper.BackupExeSqlLogMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class BackupServiceMysqlImpl extends AbstractBackupService {

    private static final String URL_PREFIX = "jdbc:mysql://";

    private static final String URL_LAST = "useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

    public BackupServiceMysqlImpl(BackupDatabase backupDatabase, BackupExeSqlLogMapper backupExeSqlLogMapper) {
        super(backupDatabase,backupExeSqlLogMapper);
    }

    /**
     * 获取数据库名列表
     * @return
     */
    @Override
    public String getGET_BASE_LIST_SQL() {
        return "select schema_name dataBaseName from information_schema.schemata";
    }

    /**
     * 获取数据库下模式名列表
     * @return
     */
    @Override
    public String getGET_BASE_SCHEMA_LIST_SQL() {
        return "SELECT schema_name schemaName FROM information_schema.schemata where schema_name not in ('information_schema','pg_catalog','pg_toast_temp_1','pg_temp_1','pg_toast') and catalog_name=? ";
    }

    /**
     * 获取表字段列表
     * @return
     */
    @Override
    public String getGET_TABLE_LIST_SQL() {
        return "select table_name tableName,table_rows rowCounts,table_comment tableComment from information_schema.tables where table_schema = ? ";
    }

    /**
     * 获取表字段列表
     * @return
     */
    @Override
    public String getGET_TABLE_FILED_LIST_SQL() {
        return "select COLUMN_NAME as filedName,COLUMN_TYPE as filedType,IS_NULLABLE as notNull,COLUMN_DEFAULT as defaultValue,COLUMN_KEY as primaryKey,COLUMN_COMMENT as filedComment from INFORMATION_SCHEMA.Columns where table_name=? and table_schema=?  ";
    }

    @Override
    public void initUrl() {
        if(StringUtils.isBlank(backupDatabase.getDatabaseName())){
            this.url = URL_PREFIX + backupDatabase.getHost() + ":" + backupDatabase.getPort();
        }else{
            this.url = URL_PREFIX + backupDatabase.getHost() + ":" + backupDatabase.getPort() + "/" + backupDatabase.getDatabaseName();
        }
        this.url +=  "?" + URL_LAST;
         this.driverClassName = DatabaseDriverClassEnum.mysql.getClassName();
    }

    @Override
    public List<String> getSchemaList(String databaseName) {
        throw new BusinessException("mysql没有模式概念");
    }

    @Override
    public String generatePageSql(String sql, Integer pageNo, Integer pageSize) {
        if(StringUtils.isBlank(sql)){
            throw new BusinessException("sql语句为空");
        }
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        String result = sql;
        int offset = (pageNo - 1) * pageSize;
        result = "select * from (" + sql + ") t limit " + offset + "," + pageSize;
        return result;
    }
}
